package com.dhcc.core.framework.lucene.util;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.*;
import org.apache.lucene.search.highlight.Highlighter;
import java.io.StringReader;

/**
 * DocumentUtil
 * 
 * @ClassName: DocumentUtil
 * @Description: TODO
 * @author: cyf
 * @date: 2018年3月22日 下午8:19:46
 */
public class DocumentUtil {

    public static Document IndexObject2Document(IndexObject indexObject) {
        Document doc = new Document();
        doc.add(new TextField("id", indexObject.getId(), Field.Store.YES));
        doc.add(new TextField("siteId", indexObject.getSiteId(), Field.Store.YES));
        doc.add(new TextField("title", indexObject.getTitle(), Field.Store.YES));
        doc.add(new TextField("keywords", indexObject.getKeywords()==null?indexObject.getTitle():indexObject.getKeywords(), Field.Store.YES));
        doc.add(new TextField("description", indexObject.getDescription()==null?indexObject.getTitle():indexObject.getDescription(), Field.Store.YES));
        doc.add(new StoredField("postDate", indexObject.getPostDate()));
        doc.add(new StoredField("url", indexObject.getUrl()));
        return doc;
    }

    public static IndexObject document2IndexObject(Analyzer analyzer, Highlighter highlighter, Document doc,
            float score) throws Exception {
        IndexObject indexObject = new IndexObject();
        indexObject.setId(doc.get("id"));
        indexObject.setSiteId(doc.get("siteId"));
        indexObject.setTitle(stringFormatHighlighterOut(analyzer, highlighter, doc, "title") == null ? doc.get("title")
                : stringFormatHighlighterOut(analyzer, highlighter, doc, "title"));
        indexObject.setKeywords(stringFormatHighlighterOut(analyzer, highlighter, doc, "keywords") == null
                ? doc.get("keywords") : stringFormatHighlighterOut(analyzer, highlighter, doc, "keywords"));
        indexObject.setDescription(stringFormatHighlighterOut(analyzer, highlighter, doc, "description") == null
                ? doc.get("description") : stringFormatHighlighterOut(analyzer, highlighter, doc, "description"));
        indexObject.setPostDate(doc.get("postDate"));
        indexObject.setUrl(doc.get("url"));
        indexObject.setScore(score);
        return indexObject;
    }

    /* 关键字加亮 */
    private static String stringFormatHighlighterOut(Analyzer analyzer, Highlighter highlighter, Document document,
            String field) throws Exception {
        String fieldValue = document.get(field);
        if (fieldValue != null) {
            TokenStream tokenStream = analyzer.tokenStream(field, new StringReader(fieldValue));
            fieldValue = highlighter.getBestFragment(tokenStream, fieldValue);
        }
        return fieldValue;
    }
}